package com.atguigu.tingshu.common.aspect;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.anno.GuiguLogin;

import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import io.swagger.v3.core.util.Json;
import jakarta.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


@Aspect   //声明当前的类是一个切面类
@Component
public class GuiguLoginAspect {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //@Around(value = "@@annotation(com.atguigu.tingshu.common.anno.GuiguLogin)")
    @Around(value = "@annotation(anntation)")
    public Object around(ProceedingJoinPoint point, GuiguLogin anntation) {

        //获取HttpServletRequest对象 可以使用httpServlet的工具类进行获取
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest httpServletRequest = requestAttributes.getRequest();
        String token = httpServletRequest.getHeader("token");
        //查询是否为受保护的页面
        boolean flag = anntation.required();

        if (flag) {

            if (StringUtils.isEmpty(token)) {
                throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
            }

            String value = redisTemplate.opsForValue().get(RedisConstant.RANKING_KEY_PREFIX + token);
            if (StringUtils.isEmpty(value)) {
                throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
            }

            UserInfo userInfo = JSON.parseObject(value, UserInfo.class);
            AuthContextHolder.setUserId(userInfo.getId());
            AuthContextHolder.setUsername(userInfo.getNickname());
        } else {
            //不是受保护的页面
            if (!StringUtils.isEmpty(token)) {
                String userInfoJson = redisTemplate.opsForValue().get(RedisConstant.RANKING_KEY_PREFIX + token);
                if (!StringUtils.isEmpty(userInfoJson)) {
                    UserInfo userInfo = JSON.parseObject(userInfoJson, UserInfo.class);
                    AuthContextHolder.setUserId(userInfo.getId());
                    AuthContextHolder.setUsername(userInfo.getNickname());
                }
            }
        }

        try {
            //执行目标方法
            Object proceed = point.proceed();

            return proceed;

        } catch (Throwable e) {
            e.getStackTrace();
        } finally {
            AuthContextHolder.removeUserId();
            AuthContextHolder.removeUsername();
        }
        return null;

    }
}
